package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Rolling;
import com.example.demo.mapper.RollingMapper;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: likew
 * @Date: 2022/4/18
 */
@RestController
@RequestMapping("/bus-rolling")
public class BusRollingController {
    @Resource
    RollingMapper rollingMapper;
    
    @PostMapping
    public Result<?> save(@RequestBody Rolling rolling) {
        rollingMapper.insert(rolling);
        return Result.success();
    }
    
    @PutMapping
    public Result<?> update(@RequestBody Rolling rolling) {
        rollingMapper.updateById(rolling);
        return Result.success();
    }
    
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        rollingMapper.deleteById(id);
        return Result.success();
    }
    
    @PostMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestBody List<Integer> ids) {
        rollingMapper.deleteBatchIds(ids);
        return Result.success();
    }
    
    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Integer id) {
        return Result.success(rollingMapper.selectById(id));
    }
    
    
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String search,
            @RequestParam(defaultValue = "") String username) {
        LambdaQueryWrapper<Rolling> wrapper = Wrappers.<Rolling>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Rolling::getProductName, search);
        }
        wrapper.eq(Rolling::getBusName,username);
        Page<Rolling> RollingPage = rollingMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(RollingPage);
    }
}
